package com.hyperzsoft.budget;

import java.util.ArrayList;

import com.hyperzsoft.budget.DBProvider.BudgetTable;
import com.hyperzsoft.budget.DBProvider.ExpenseType;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

@SuppressWarnings("unused")
public class ExpenseGrid extends Activity {
	
	static private String TAG = "ExpenseGrid";
	private DBProvider dbProvider;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.credit_grid);
		
		dbProvider = new DBProvider(this.getApplicationContext());
	}
	
	@Override
	public void onSaveInstanceState(Bundle instanceState) {
		super.onSaveInstanceState(instanceState);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.expense_grid_menu, menu);
			return true;
		} catch (Exception e) {
			Log.e(TAG, e.getStackTrace().toString());
			return false;
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			// Handle item selection
			switch (item.getItemId()) {
			case R.id.expense_type_list:
				GridView gridview = (GridView) findViewById(R.id.BudgetList);
			    gridview.setAdapter(new expenseGridAdapter(getApplicationContext()));
			    return true;
			case R.id.quit:
				finish();
				return true;
			case R.id.create_db:
				//createDefaultGroupsAndItems();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		} catch (Exception e) {
			Log.e(TAG, e.toString());
			return false;
		}
	}
	
	public class expenseGridAdapter extends BaseAdapter {
		private ArrayList<Object> workingList;
		private ArrayList<ExpenseType> expenseList = new ArrayList<ExpenseType>();
		private DBProvider db_table;
		
		public expenseGridAdapter(Context context) {
			db_table = new DBProvider(context);
			workingList = db_table.selectAll(BudgetTable.EXPENSE_TYPE, null, null, null);
			
			//Argggh.. no way to cast the array list into a specific ArrayList of budget objects??
			for (Object obj : workingList){
				expenseList.add((ExpenseType)obj);
			}
		}
		
		public int getCount() {
		    return expenseList.size();
		}
		
		public Object getItem(int position) {
		    return null;
		}
		
		public long getItemId(int position) {
		    return 0;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		    View itemView;
		    
		    if (convertView == null) {
		    	LayoutInflater li = getLayoutInflater();
		    	itemView = li.inflate(R.layout.expense_item, null);
		    	
		    	TextView id = (TextView)itemView.findViewById(R.id.expense_group_id);
		    	id.setText(Long.toString(expenseList.get(position)._ID));
		    	
		    	TextView amount = (TextView)itemView.findViewById(R.id.expense_group_name);
		    	amount.setText(expenseList.get(position).NAME);
		    	
		    	amount.setBackgroundColor(expenseList.get(position).COLOR);
		    }
		    else {
		    	itemView = (View) convertView;
		    }
		    
		    return itemView;
		}
	}
}